package com.wytec.xywzxw.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.wytec.xywzxw.bean.commonBean.PageSizeBean;
import com.wytec.xywzxw.bean.User;
import com.wytec.xywzxw.configuration.MyException;
import com.wytec.xywzxw.constant.MyConstant;
import com.wytec.xywzxw.mapper.UserMapper;
import com.wytec.xywzxw.service.UserService;
import com.wytec.xywzxw.utils.CommonUtils;
import com.wytec.xywzxw.utils.redis.RedisUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    RedisUtils redisUtils;
    @Resource
    UserMapper userMapper;

    @Override
    public List<User> getUserList(PageSizeBean page, String keyword) {
        QueryWrapper<User>queryWrapper =  new QueryWrapper<>();
        if (keyword != null && !keyword.isEmpty()){
            queryWrapper.like("username",keyword).or().like("mobile",keyword);
        }
        PageHelper.startPage(page.getPage(), page.getSize());
        List<User> users = userMapper.selectList(queryWrapper);
        for (User user:users) {
            user.setToken(null);
            user.setPassword(null);
        }
        return users;
    }

    @Override
    public User userLogin(String username, String password) throws MyException {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user = userMapper.selectOne(queryWrapper);
        if (user != null){
            if (!user.getPassword().equals(password)){
                throw new MyException("密码错误");
            }else if (user.getStatus() == 0){
                throw new MyException("账号已禁用");
            }
        }else {
            throw new MyException("账号不存在");
        }
        String token = CommonUtils.createToken();
        // 更新用户表数据
        user.setToken(token);
        user.setLoginCount(user.getLoginCount()+1);
        redisUtils.set(MyConstant.TOKEN+":"+token,JSON.toJSONString(user));

        user.setLastLoginIp(CommonUtils.getIpAddr());
        user.setLastLoginTime(new Date());
        userMapper.updateById(user);
        // 存入缓存
        return user;
    }
    @Override
    public User userSignIn(User user) throws MyException {
        user.setStatus(1);
        try{
            userMapper.insert(user);
        }catch (Exception e){
            throw new MyException("注册失败");
        }
        return user;
    }

    @Override
    public User getUserInfo(String token) {
        String userInfo = redisUtils.get(MyConstant.TOKEN + ":" + token);
        User user = JSON.parseObject(userInfo,User.class);
        return user;
    }

    @Override
    public Boolean logOut(String token) {
        String key = MyConstant.TOKEN + ":" + token;
        Long del = redisUtils.del(key);
        return del != 0;
    }
}
